<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	disk.php - Disk Cache Driver
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class cacheSlave extends cacheMaster
{
	public $driver = 'Disk Cache';
	public $driver_version = '1.0 PF';
	public $driver_author = 'Matix';

	public $dependency = '';

	function add($key, $data, $ttl = 0)
	{
		$headers = 
'/**
 * Smit Auto-generated Cache
 * Please do not modify this file, weird things can happen....
 * GENERATED AT: ' . date('r') . ', Have a nice day :)
 */';
		$str = "<?php\n\n{$headers}\n\n\$ttl = {$ttl};\n\$data = '";
		$str .= addslashes(serialize($data));
		$str .= "';\n\n?>";
		file_put_contents(SMIT_PATH . 'cache/' . md5($key) . EXT, $str);
		
		@chmod(SMIT_PATH . 'cache/' . md5($key) . EXT, $str, 0666);
	}
	
	function get($key)
	{
		if(!file_exists(SMIT_PATH . 'cache/' . md5($key) . EXT)) return FALSE;

		require SMIT_PATH . 'cache/' . md5($key) . EXT;
		if($ttl > 0 && ((filemtime(SMIT_PATH . 'cache/' . md5($key) . EXT) + $ttl) < time()))
		{
			$this->delete($key);
			return FALSE;
		}
		return unserialize(stripslashes($data));
	}
	
	public function delete($key)
	{
		if(!file_exists(SMIT_PATH . 'cache/' . md5($key) . EXT)) return FALSE;

		unlink(SMIT_PATH . 'cache/' . md5($key) . EXT);
	}
}

?>